package com.food.restaurantpro.fragments;

import java.util.Random;

import com.food.restaurantpro.R;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.utils.Utils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class ReservationAddMoreInfoFragment extends Fragment implements OnClickListener {
	
	private TextView tvHeader;
	private TextView mTvCapcharCode;
	private String mStrCapcharCode = "";
	private EditText mEtName;
	private EditText mEtEmail;
	private EditText mEtPhone;
	private EditText mEtCapcharCode;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mStrCapcharCode += (new Random().nextInt(1000000) + 1000000);
		mStrCapcharCode = mStrCapcharCode.substring(1);
		View view = inflater.inflate(R.layout.fragment_reservation_guest, container, false);
		
		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		if(tvHeader != null) {
			tvHeader.setText(R.string.reservation);
		}
		
		view.findViewById(R.id.gl_tv_content).setOnClickListener(this);
		view.findViewById(R.id.btnSubmitReservationGuest).setOnClickListener(this);
		view.findViewById(R.id.gl_tv_title).setOnClickListener(this);
		view.findViewById(R.id.btnCancelReservationGuest).setOnClickListener(this);
		
		mTvCapcharCode = (TextView) view.findViewById(R.id.tvYourCodeReservationGuest);
		
		mEtName = (EditText) view.findViewById(R.id.edtYourNameReservationGuest);
		mEtEmail = (EditText) view.findViewById(R.id.edtYourEmailReservationGuest);
		mEtPhone = (EditText) view.findViewById(R.id.edtYourPhoneReservationGuest);
		mEtCapcharCode = (EditText) view.findViewById(R.id.edtYourCodeReservationGuest);
		mTvCapcharCode.setText(mStrCapcharCode);
		return view;
	}
	
	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		Runtime.getRuntime().gc();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gl_tv_content:
		case R.id.btnSubmitReservationGuest:
			gotoNextStep();
			break;
		case R.id.gl_tv_title:
		case R.id.btnCancelReservationGuest:
			cancel();
			break;
		default:
			break;
		}
	}


	private void gotoNextStep() {
		String name = mEtName.getText().toString();
		String email = mEtEmail.getText().toString();
		String phone = mEtPhone.getText().toString();
		String capchar = mEtCapcharCode.getText().toString();
		if(Utils.isEmpty(name)) {
			Utils.showToast(getString(R.string.pleaseInputName));
			return;
		} else if(Utils.isEmpty(email) || !Utils.isValidEmail(email)) {
			Utils.showToast(getString(R.string.pleaseInputEmail));
			return;
		} else if(Utils.isEmpty(phone)) {
			Utils.showToast(getString(R.string.pleaseInputPhone));
			return;
		} else if(Utils.isEmpty(capchar) || !capchar.equals(mStrCapcharCode)) {
			Utils.showToast(getString(R.string.pleaseInputCapchar));
			return;
		}
		((MainActivity)getActivity()).showNextReservationFragment();
	}


	private void cancel() {
		((MainActivity)getActivity()).showSaleFragment();
	}
}
